﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using Ryder_US_Website.BUS;

namespace Ryder_US_Website.Presentation.Admin
{
    public partial class Cargo_Update : System.Web.UI.Page
    {
        public string id;
        RUS_Cargo car = new RUS_Cargo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["RUS_User"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            id = Request.QueryString["id"];
            int temp = Int16.Parse(id);
            car = CargoBUS.GetCargoByID(temp);
            if (!IsPostBack)
            {
                // Load Service
                List<RUS_ServiceDetail> service = ServiceDetailBUS.GetAll();
                DropDownListTenDichVu.DataSource = service;
                DropDownListTenDichVu.DataTextField = "ServiceDetail_name";
                DropDownListTenDichVu.DataValueField = "ServiceDetail_ID";
                DropDownListTenDichVu.DataBind();

                //Load Customer
                List<RUS_Customer> customer = CustomerBUS.GetAll();
                DropDownListCustomer.DataSource = customer;
                DropDownListCustomer.DataTextField = "Customer_name";
                DropDownListCustomer.DataValueField = "Customer_ID";
                DropDownListCustomer.DataBind();

                //Load CargoStatus
                List<RUS_CargoStatus> status = CargoStatusBUS.GetAll();
                DropDownListCargoStatus.DataSource = status;
                DropDownListCargoStatus.DataTextField = "CargoStatus_name";
                DropDownListCargoStatus.DataValueField = "CargoStatus_ID";
                DropDownListCargoStatus.DataBind();

                txtSoLuong.Text = car.Cargo_count.ToString();
                txtNguoiNhan.Text = car.Cargo_receiver;
                txtDiaChi.Text = car.Cargo_receiveAddress;
                txtDienThoai.Text = car.Cargo_receivePhone;
                txtGhiChu.Text = car.Cargo_description;
                txtTongTien.Text = car.Cargo_total.ToString();
                txtSoTienTra.Text = car.Cargo_paidMoney.ToString();
                rbChuaThanhToan.Checked = (car.Cargo_isPaid == 0);
                rbDaThanhToan.Checked = !rbChuaThanhToan.Checked;
                rbChuaHuy.Checked = (car.Cargo_trash == 0);
                rbDaHuy.Checked = !rbChuaHuy.Checked;
                for (int i = 0; i < DropDownListTenDichVu.Items.Count; i++)
                {
                    if (car.Cargo_serviceDetailID.ToString().Equals(DropDownListTenDichVu.Items[i].Value))
                        DropDownListTenDichVu.SelectedIndex = i;
                }
                for (int i = 0; i < DropDownListCustomer.Items.Count; i++)
                {
                    if (car.Cargo_sender.ToString().Equals(DropDownListCustomer.Items[i].Value))
                        DropDownListCustomer.SelectedIndex = i;
                }
                for (int i = 0; i < DropDownListCargoStatus.Items.Count; i++)
                {
                    if (car.Cargo_status.ToString().Equals(DropDownListCargoStatus.Items[i].Value))
                        DropDownListCargoStatus.SelectedIndex = i;
                }

            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("Cargo_List.aspx");
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            RUS_Cargo cargo=new RUS_Cargo();
            car.Cargo_trash = (rbDaHuy.Checked == true) ? 1 : 0;
            car.Cargo_count = Int16.Parse(txtSoLuong.Text);
            car.Cargo_receiver = txtNguoiNhan.Text;
            car.Cargo_receivePhone = txtDienThoai.Text;
            car.Cargo_receiveAddress = txtDiaChi.Text;
            car.Cargo_total = Decimal.Parse(txtSoTienTra.Text);
            car.Cargo_paidMoney = Decimal.Parse(txtSoTienTra.Text);
            car.Cargo_isPaid=(rbChuaThanhToan.Checked == true) ? 0 : 1;
            car.Cargo_sender = Int16.Parse(DropDownListCustomer.SelectedItem.Value);
            car.Cargo_serviceDetailID = int.Parse(DropDownListTenDichVu.SelectedItem.Value);
            car.Cargo_status = int.Parse(DropDownListCargoStatus.SelectedItem.Value);
            try
            {
                cargo = CargoBUS.Update(car.Cargo_ID,car.Cargo_serviceDetailID, car.Cargo_count, car.Cargo_sender,
                                        car.Cargo_receiver, car.Cargo_receivePhone,car.Cargo_receiveAddress, car.Cargo_description, car.Cargo_total,
                                         cargo.Cargo_paidMoney,car.Cargo_status,car.Cargo_isPaid,car.Cargo_trash);
            }
            catch (Exception ex)
            {
                Response.Write(Utils.UtilHelpers.ShowMessage(ex.Message));
            }
            if (car.Cargo_ID.Equals(cargo.Cargo_ID))
            {
                Response.Redirect("Cargo_List.aspx");
            }
            else
            {
                Response.Write(Utils.UtilHelpers.ShowMessage("Cập nhật đơn hàng thất bại"));
            }
        }
    }
}
